package extend_widget

import (
	"bytes"
	"clientDemo/ui/fyne_ui/common"
	"clientDemo/ui/fyne_ui/widget"
	"clientDemo/ui/fyne_ui/widget/interface"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	"github.com/ajstarks/svgo"
	"github.com/fogleman/gg" // 用于处理 SVG
	_ "github.com/fyne-io/oksvg"
	"image"
	"image/color"
	"image/draw"
	"image/png"
	"os"
	"os/exec"
)

var _ widgetcommon.IWidgetItem = (*Image)(nil)

func init() {
	widget.RegWidget("WidgetSvgImage", "Svg图片", NewSvgImage)
}

type SvgImage struct {
	*WidgetContainer
	Image *canvas.Image
	*Action
	rootNode widgetcommon.IWidgetItem //父节点
	name     string
}

func NewSvgImage(name string, skinInfo widgetcommon.IWidgetSkin, rootNode widgetcommon.IWidgetItem) widgetcommon.IWidgetItem {
	if skinInfo == nil {
		return nil
	}
	o := &canvas.Image{}
	o.FillMode = canvas.ImageFillOriginal
	_image := &SvgImage{
		Image:  o,
		Action: &Action{},
		WidgetContainer: &WidgetContainer{
			Container: &Container{
				name: name,
			},
			Border: &canvas.Rectangle{
				FillColor:   color.Transparent,
				StrokeColor: color.RGBA{255, 0, 0, 255}, //黄色
				StrokeWidth: 2,
			},
		},
		rootNode: rootNode,
	}
	_image.WidgetContainer.ac = _image.Action
	_image.IUserInfo = common.NewWidgetUserInfo(name, skinInfo.GetSkinName(), _image)
	_image.WidgetContainer.SetObjects([]fyne.CanvasObject{_image.Image})
	_image.SetBase(_image, rootNode)
	_image.ExtendBaseWidget(_image)
	_image.name = name

	return _image
}
func (i *SvgImage) SetProperty(key, value string, isProperty bool) {
	i.IUserInfo.SetProperty(key, value, isProperty)
	if key == "ImagicTexture" {
		i.SetImagePath(value)
	}
}
func (i *SvgImage) SetImagePath(path string) {
	//i.Image.File = path // 设置图片路径
	/*	// 加载SVG文件
		file, err := oksvg.ReadImageFile("example.svg")
		if err != nil {
			panic(err)
		}

		// 创建一个图像对象
		image := canvas.NewImageFromResource(file)
		i.Image = image*/
	/*// Generate SVG
	var buf bytes.Buffer
	svgCanvas := svg.New(&buf)
	svgCanvas.Start(200, 200)
	svgCanvas.Circle(100, 100, 80, "fill:red")
	svgCanvas.Text(100, 100, i.name, "font-size:20px;text-anchor:middle;fill:white")
	svgCanvas.End()

	// Convert SVG to PNG
	imgData, err := svgToPNG(&buf)
	if err != nil {
		log.Fatal(err)
	}
	r := bytes.NewReader(imgData)
	// Create a resource from the PNG image
	i.Image = canvas.NewImageFromReader(r, i.name)*/
	// 生成SVG字符串
	svgContent := `<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
        <circle cx="100" cy="100" r="50" fill="blue" />
    </svg>`

	// 将SVG字符串转换为图像
	img := canvas.NewImageFromResource(nil)
	img.Resize(fyne.NewSize(200, 200))
	img.SetMinSize(fyne.NewSize(200, 200))
	img.FillMode = canvas.ImageFillOriginal

	// 将SVG内容转换为图像数据
	svgBytes := []byte(svgContent)
	imgResource := fyne.NewStaticResource("svgimage", svgBytes)
	img1 := canvas.NewImageFromResource(imgResource)
	img1.SetMinSize(fyne.NewSize(200, 200))
	i.Image = img1    // generateImage()
	i.Image.Refresh() // 刷新控件以显示新图片
}

func (i *SvgImage) MinSize() fyne.Size {
	return i.Image.MinSize()
	//i.ExtendBaseWidget(i)
	//return i.BaseWidget.MinSize()
}
func svgToPNG(svgBuf *bytes.Buffer) ([]byte, error) {
	// Use an external tool to convert SVG to PNG
	cmd := exec.Command("inkscape", "--export-type=png", "--export-filename=output.png", "-")
	cmd.Stdin = svgBuf
	if err := cmd.Run(); err != nil {
		return nil, err
	}

	// Read the generated PNG file
	imgData, err := os.ReadFile("output.png")
	if err != nil {
		return nil, err
	}

	return imgData, nil
}

// generateImage 生成带有折线的图像
func generateImage() *canvas.Image {
	// 创建图像画布
	img := image.NewRGBA(image.Rect(0, 0, 200, 200))
	draw.Draw(img, img.Bounds(), &image.Uniform{color.White}, image.Point{}, draw.Src)

	// 使用 bytes.Buffer 创建内存中的 SVG
	var buf bytes.Buffer
	svgContent := svg.New(&buf)
	svgContent.Start(200, 200)
	svgContent.Circle(100, 100, 80, "fill:red")
	svgContent.Text(100, 100, "asads", "font-size:20px;text-anchor:middle;fill:white")

	// 绘制折线
	for i := 0; i < 2; i++ {
		x1, y1 := 10+i*20, 10
		x2, y2 := 10+i*20, 190
		svgContent.Line(x1, y1, x2, y2, "stroke:black;stroke-width:2")
	}

	svgContent.End()

	// 使用 gg 库将 SVG 渲染为图像
	o := convertSVGToImage(buf.String())
	o.SetMinSize(fyne.NewSize(200, 200))
	return o
}

// convertSVGToImage 将 SVG 字符串转换为 image.Image
func convertSVGToImage(svgStr string) *canvas.Image {
	const width = 200
	const height = 200
	dc := gg.NewContext(width, height)

	// 使用 gg 渲染 SVG
	dc.DrawString(svgStr, float64(width), float64(height))

	// 返回图像
	img := dc.Image()
	buf := new(bytes.Buffer)
	png.Encode(buf, img) // 将图像编码为 PNG
	i := &canvas.Image{
		Image: img,
	}
	return i
	//return canvas.NewImageFromReader(buf, "image/png")
}
